package com.atguigu.admin.config;

import com.atguigu.admin.interceptor.LoginInterceptor;
import com.atguigu.admin.interceptor.RedisUrlCountInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 1.编写一个拦截器实现HandlerInterceptor
 * 2.拦截器注册到容器中(实现WebMvcConfigurer#addInterceptors 方法)
 * 3.指定拦截规则[如果是拦截所有,静态资源也会被拦截]
 */
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {


    /**
     *
     * 从容器中拿
     * Filter, Interceptor 几乎都有相同的功能?
     * 用哪个好
     * 
     * Filter是Servlet定义的原生组件,脱离了Spring应用也能使用
     * Interceptor是Spring定义的接口,在Spring里面用.可以使用Spring的自动装配等功能
     */
    @Autowired
    RedisUrlCountInterceptor redisUrlCountInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(new LoginInterceptor())
               .addPathPatterns("/**") // 静态所有请求都会被拦截,包括静态资源
               .excludePathPatterns("/","/login","/js","/css/**","/fonts/**","/images/**","/js/**") // 放行的请求
               ;

       // redis计数的Interceptor
       registry.addInterceptor(redisUrlCountInterceptor)
               .addPathPatterns("/**") // 静态所有请求都会被拦截,包括静态资源
               .excludePathPatterns("/","/login","/js","/css/**","/fonts/**","/images/**","/js/**") // 放行的请求
       ;
    }
}
